<template>
    <div class="shopcart">
      <div class="content" @click="toggleList">
        <div class="content-left">
          <div class="logo-wrapper">
            <!-- 根据totalCount也就是食物数量是否为0判断控制logo是否高亮 -->
            <div class="logo" :class="{'highlight':totalCount>0}">
              <i class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></i>
            </div>
            <div class="num" v-show="totalCount>0">
              <bubble :num="totalCount"></bubble>
            </div>
          </div>
          <!-- totalPrice总价，通过总价控制价格高亮 -->
          <div class="price" :class="{'highlight':totalPrice>0}">￥{{totalPrice}}</div>
          <div class="desc">另需配送费￥{{deliveryPrice}}元</div>
        </div>
         <!-- 显示达到起送金额能否付款，如果能显示付款数量同时改变样式 -->
        <div class="content-right" @click.="pay">
          <div class="pay" click="pay" :class="payClass">
            {{payDesc}}
          </div>
        </div>
      </div>
      <div class="ball-container">
        <div v-for="(ball,index) in balls" :key="index">
          <transition name="drop" 
            @before-enter="beforeDrop" 
            @enter="dropping" 
            @after-enter="afterDrop">
            <div class="ball" v-show="ball.show">
              <div class="inner inner-hook"></div>
            </div>
          </transition>
        </div>
      </div>
    </div>
</template>

<script type="text/ecmascript-6">
  import bubble from "components/bubble/bubble"
  //创建隐藏小球
  const  BALL_LEN=10
  function createBall(){
    let ret = []
    for(let i=0;i<BALL_LEN;i++){
      ret.push({
        show:false
      })
    }
    return ret
  }
  export default {
    props: {
      selectFoods: {
        type: Array,
        default() {
          return [];
        }
      },
      deliveryPrice: {
        type: Number,
        default: 0
      },
      minPrice: {
        type: Number,
        default: 0
      },
      fold:{
        type:Boolean,
        default:true
      },
      sticky:{
        type:Boolean,
        default:false
      }
    },
    data() {
      return {
        balls:createBall(),
        dropBalls:[],
        //不能直接改变props内的属性
        listFold:this.fold,
        listcom:{}
      };
    },
    computed: {
      //计算总价
      totalPrice() {
        let total = 0;
        this.selectFoods.forEach((food) => {
          total += food.price * food.count;
        });
        return total;
      },
      //计算总数
      totalCount() {
        let count = 0;
        this.selectFoods.forEach((food) => {
          count += food.count;
        });
        return count;
      },
      payDesc() {
        //是否可以配送
        if (this.totalPrice === 0) {
          return `￥${this.minPrice}元起送`;
        } else if (this.totalPrice < this.minPrice) {
          let diff = this.minPrice - this.totalPrice;
          return `还差￥${diff}元起送`;
        } else {
          return '去结算';
        }
      },
      payClass() {
        if (this.totalPrice < this.minPrice) {
          return 'not-enough';
        } else {
          return 'enough';
        }
      }
    },
    methods: {
      drop(el) {
        for (let i = 0; i < this.balls.length; i++) {
          let ball = this.balls[i];
          //把小球变成显示状态，并且添加进掉落数组
          if (!ball.show) {
            ball.show = true;
            ball.el = el;
            this.dropBalls.push(ball);
            return ;
          }
        }
      },
      beforeDrop(el) {
        //将小球位置初始化到添加按钮位置
        const ball = this.dropBalls[this.dropBalls.length-1]
        console.log(this.dropBalls,ball)
        let rect = ball.el.getBoundingClientRect();
        let x = rect.left - 32;
        let y = -(window.innerHeight - rect.top - 22);
        el.style.display = '';
        //webkitTransform解决谷歌兼容
        //两个动画内层和外层同时运动形成抛物线
        el.style.webkitTransform = `translate3d(0,${y}px,0)`;
        el.style.transform = `translate3d(0,${y}px,0)`;
        //获取内层
        let inner = el.getElementsByClassName('inner-hook')[0];
        inner.style.webkitTransform = `translate3d(${x}px,0,0)`;
        inner.style.transform = `translate3d(${x}px,0,0)`;
      },
      dropping(el, done) {
        //重绘页面
        this._reflow = document.body.offsetHeight;
        //抛物线动画
        el.style.webkitTransform = 'translate3d(0,0,0)';
        el.style.transform = 'translate3d(0,0,0)';
        let inner = el.getElementsByClassName('inner-hook')[0];
        inner.style.webkitTransform = 'translate3d(0,0,0)';
        inner.style.transform = 'translate3d(0,0,0)';
        //监听完成动画，触发结束钩子
        el.addEventListener('transitionend', done);
      },
      afterDrop(el) {
        //回收小球
        let ball = this.dropBalls.shift();
        if (ball) {
          ball.show = false;
          el.style.display = 'none';
        }
      },
      toggleList(){
        console.log(this.listFold)
        //判断是否是收起状态
        if(this.listFold){
          //判断购物车是否为空
          if(!this.totalCount){
            //如果为空跳出方法
            return 
          }
          //将状态切换
          this.listFold=false
          //调用显示购物列表
          this.showCarList()
          this.showCartSticky()
        }else{
          //切换为隐藏状态
          this.listFold=true
          this.hideCarList()
        }
      },
      showCarList(){
        //利用createAPI生成购物列表蒙板
        this.shopCarListComp=this.shopCarListComp || this.$createShopCarList({
          $props:{
            selectFoods:"selectFoods",
          },
          $events:{
          //$events是createAPI中第二个参数，可以实现类似于事件发送，子组件调用父组件方法
            hide:()=>{
              //通过事件发送监听修改父组件中的显示状态
              this.listFold=true
              },
            afterLeave:()=>{
              //动画完成后隐藏sticky
              this.hideCartSticky()
            },
            add:(el)=>{
              this.showCartStickyComp.drop(el)
            }
          } 
        })
        this.shopCarListComp.show()
      },
      showCartStickyDrop(el){
        this.showCartStickyComp.drop(el)
      },
      hideCarList(){
        //如果是复刻的sticky组件是没有创建过shopCarListComp的，要把这个组件传给sticky组件调用shopCarListComp的方法才能关闭，所以我们通过一个list属性把shopCarListComp传给sticky
        // if(this.sticky){
        //   console.log("sticky"+this.sticky)
        //   console.log(this)
        //   this.$parent.list.hide()
        // }else{
        //    this.shopCarListComp.hide()
        // }
        // this.shopCarListComp.hide()
        //this.$parent获取当前组件父级实例所有属性
        const comp =this.sticky?this.$parent.list:this.shopCarListComp
        comp.hide && comp.hide()
      },
      showCartSticky(){
        //创建CartSticky组件
        this.listcom=this.shopCarListComp
        this.showCartStickyComp = this.showCartStickyComp || this.$createShowCartSticky({
          $props:{
              selectFoods:"selectFoods",
              deliveryPrice:"deliveryPrice",
              minPrice:"minPrice",
              fold:"listFold",
              //把shopCarListComp利用list传给sticky
              list:"listcom"
          }
        })
        this.showCartStickyComp.show()
      },
      hideCartSticky(){
        this.showCartStickyComp.hide()
      },
      pay(e){
        if(this.totalPrice<this.minPrice){
          return
        }
        this.$createDialog({
          title:"支付",
          content:`支付￥${this.totalPrice}愿`
        }).show()
         e.stopPropagation()
      }
    },
    watch:{
      //flod是通过props传过来的，需要监听fold，及时更新fold的值给listFold
      fold(newVal){
        this.listFold=newVal
      },
      totalCount(newVal){
        if(!this.listFold&&!newVal){
          this.hideCarList()
        }
      }
    },
    components: {
      bubble
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixin.styl"
  .shopcart
    width :inherit
    position: absolute
    left: 0
    bottom: 0
    z-index: 50
    height: 48px
    .content
      height:100%
      display: flex
      justify-content : space-between
      background: #141d27
      font-size: 0
      color: rgba(255, 255, 255, 0.4)
      .content-left
        .logo-wrapper
          display: inline-block
          vertical-align: top
          position: relative
          top: -10px
          margin: 0 12px
          padding: 6px
          width: 56px
          height: 56px
          box-sizing: border-box
          border-radius: 50%
          background: #141d27
          .logo
            width: 100%
            height: 100%
            border-radius: 50%
            text-align: center
            background: #2b343c
            &.highlight
              background: rgb(0, 160, 220)
            .icon-shopping_cart
              line-height: 44px
              font-size: 24px
              color: #80858a
              &.highlight
                color: #fff
          .num
            position: absolute
            top: 0
        .price
          display: inline-block
          vertical-align: top
          margin-top: 12px
          line-height: 24px
          padding-right: 12px
          box-sizing: border-box
          border-right: 1px solid rgba(255, 255, 255, 0.1)
          font-size: 16px
          font-weight: 700
          &.highlight
            color: #fff
        .desc
          display: inline-block
          vertical-align: top
          margin: 12px 0 0 12px
          line-height: 24px
          font-size: 10px
      .content-right
        width :105px
        .pay
          height: 48px
          line-height: 48px
          text-align: center
          font-size: 12px
          font-weight: 700
          &.not-enough
            background: #2b333b
          &.enough
            background: #00b43c
            color: #fff
    .ball-container
      .ball
        position: fixed
        left: 32px
        bottom: 22px
        z-index: 200
        //cubic-bezier先向上
        -webkit-transition: all 0.4s cubic-bezier(0.49, -0.29, 0.75, 0.41)
        .inner
          width: 16px
          height: 16px
          border-radius: 50%
          background: rgb(0, 160, 220)
          -webkit-transition: all 0.4s linear
    .shopcart-list
      position: absolute
      left: 0
      top: 0
      z-index: -1
      width: 100%
      transform: translate3d(0, -100%, 0)
      &.fold-enter-active, &.fold-leave-active
        transition: all 0.5s
      &.fold-enter, &.fold-leave-active
        transform: translate3d(0, 0, 0)
      .list-header
        height: 40px
        line-height: 40px
        padding: 0 18px
        background: #f3f5f7
        border-bottom: 1px solid rgba(7, 17, 27, 0.1)
        .title
          float: left
          font-size: 14px
          color: rgb(7, 17, 27)
        .empty
          float: right
          font-size: 12px
          color: rgb(0, 160, 220)

      .list-content
        padding: 0 18px
        max-height: 217px
        overflow: hidden
        background: #fff
        .food
          position: relative
          padding: 12px 0
          box-sizing: border-box
          border-1px(rgba(7, 17, 27, 0.1))
          .name
            line-height: 24px
            font-size: 14px
            color: rgb(7, 17, 27)
          .price
            position: absolute
            right: 90px
            bottom: 12px
            line-height: 24px
            font-size: 14px
            font-weight: 700
            color: rgb(240, 20, 20)
          .cartcontrol-wrapper
            position: absolute
            right: 0
            bottom: 6px

  .list-mask
    position: fixed
    top: 0
    left: 0
    width: 100%
    height: 100%
    z-index: 40
    backdrop-filter: blur(10px)
    opacity: 1
    background: rgba(7, 17, 27, 0.6)
    &.fade-enter-active, &.fade-leave-active
      transition: all 0.5s
    &.fade-enter, &.fade-leave-active
      opacity: 0
      background: rgba(7, 17, 27, 0)
</style>